import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/choice_bar.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/module/match/detail/match_odds_logic.dart';
import 'package:ball_lion/entity/match/zq_odds_entity.dart';
import 'package:ball_lion/widgets/ads_label.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class ZqMatchOddsView extends StatefulWidget {
  const ZqMatchOddsView({Key? key}) : super(key: key);

  @override
  State<ZqMatchOddsView> createState() => _ZqMatchOddsViewState();
}

class _ZqMatchOddsViewState extends State<ZqMatchOddsView> {
  final logic = Get.put(ZqMatchOddsLogic(), tag: '${Get.arguments}');

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZqMatchOddsLogic>(
        tag: '${Get.arguments}',
        builder: (logic) {
          return Column(
            children: [
              Container(
                  color: Colours.white,
                  padding:
                      const EdgeInsets.symmetric(horizontal: 16, vertical: 5),
                  child: ChoiceBar(
                      items: logic.typeList,
                      backgroundColor: Colours.redFEECEC,
                      textColor: Colours.main,
                      width: 76,
                      onTap: (index) {
                        Utils.onEvent("zqbsxq_zs",
                            params: {"zqbsxq_zs": index});
                        logic.typeIndex = index;
                      })),
              Expanded(
                child: EasyRefresh.builder(
                  onRefresh: () async {
                    await logic.requestData();
                  },
                  header:
                      Styles.refreshHeader(position: IndicatorPosition.locator),
                  childBuilder: (context, physics) {
                    return TabBarView(
                        physics: const NeverScrollableScrollPhysics(),
                        controller: logic.tabController,
                        children: List.generate(
                          logic.typeList.length,
                          (index) =>
                              CustomScrollView(physics: physics, slivers: [
                            const HeaderLocator.sliver(),
                            SliverToBoxAdapter(
                              child: AdsLabel(
                                adsTag: "match_zq_指数_$index",
                                tapType: 'ggw_bsxq_zs',
                                margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
                              ),
                            ),
                            LoadingWidget<bool>(
                              isLoading: false,
                              data: logic.data[index]?.length == 0,
                              noData: NoDataWidget.sliver(
                                  tip: "暂无${logic.typeList[index]}数据"),
                              child: () => SliverToBoxAdapter(
                                child: Styles.card(
                                  color: Colours.white,
                                  margin: const EdgeInsets.only(
                                      top: 8, bottom: 30, left: 8, right: 8),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: [
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Expanded(
                                              flex: 2,
                                              child: companyList(index)),
                                          Expanded(
                                              flex: 9, child: valueList(index)),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ]),
                        ));
                  },
                ),
              )
            ],
          );
        });
  }

  Widget companyList(int index) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
          (logic.data[index]?.length ?? 0) + 1,
          (i) => Container(
                      height: i == 0 ? 32 : 60,
                      alignment: Alignment.centerLeft,
                      margin: const EdgeInsets.only(left: 12),
                      decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 0.5, color: Colours.greyEE))),
                      child: QsText(
                          i == 0
                              ? "公司"
                              : logic.data[index]?[i - 1].companyName ?? "",
                          fontSize: 11))
                  .tap(() {
                if (i != 0) {
                  Utils.onEvent("zqbsxq_zs", params: {"zqbsxq_zs": 4});
                  Get.toNamed(Routes.zqOddsDetail, arguments: [
                    logic.data[index]?[i - 1].companyId,
                    logic.detail.matchId,
                    index
                  ]);
                }
              })),
    );
  }

  Widget valueList(int index) {
    return Padding(
        padding: const EdgeInsets.only(right: 16, bottom: 6),
        child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: List.generate(
                  (logic.data[index]?.length ?? 0) + 1,
                  (i) => i == 0
                      ? header(index)
                      : item(logic.data[index]?[i - 1], index)),
            )));
  }

  Widget item(ZqOddsEntity? entity, int index) {
    var firstData = logic.formSingleLine(entity?.firstData ?? []);
    var lastData = logic.formSingleLine(entity?.lastData ?? []);
    return GestureDetector(
      onTap: () => Get.toNamed(Routes.zqOddsDetail,
          arguments: [entity?.companyId, logic.detail.matchId, index]),
      child: Container(
        height: 60,
        padding: const EdgeInsets.symmetric(vertical: 10),
        decoration: BoxDecoration(
            border: logic.data.length == index
                ? null
                : const Border(
                    bottom: BorderSide(width: 0.5, color: Colours.greyEE))),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (index == 2)
                  SizedBox(
                    width: 30,
                    child: QsText(entity?.line ?? "", fontSize: 12).center,
                  ),
                Container(
                    width: 30,
                    alignment: Alignment.center,
                    child: QsText("初", fontSize: 11, color: Colours.grey99)),
                Container(
                    width: 52,
                    alignment: Alignment.center,
                    child: firstData[0]),
                Container(
                  width: 52,
                  alignment: Alignment.center,
                  child: (index == 1 || index == 3
                      ? QsText(entity?.line ?? "", fontSize: 12)
                      : firstData[1]),
                ),
                Container(
                    width: 52,
                    alignment: Alignment.center,
                    child: firstData[2]),
                Container(
                    width: 110,
                    alignment: Alignment.center,
                    child: firstData[3]),
                Container(
                  width: 60,
                  alignment: Alignment.center,
                  child: QsText(
                      entity?.firstRate.noValue == true
                          ? ""
                          : "${(double.parse(entity!.firstRate!) * 100).toStringAsFixed(2)}%",
                      fontSize: 12),
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (index == 2)
                  Container(
                    width: 30,
                    alignment: Alignment.center,
                    child: QsText(entity?.line ?? "", fontSize: 12),
                  ),
                Container(
                    width: 30,
                    alignment: Alignment.center,
                    child: QsText("即", fontSize: 11, color: Colours.grey99)),
                Container(
                    width: 52, alignment: Alignment.center, child: lastData[0]),
                Container(
                  width: 52,
                  alignment: Alignment.center,
                  child: (index == 1 || index == 3
                      ? QsText(entity?.line ?? "", fontSize: 12)
                      : lastData[1]),
                ),
                Container(
                    width: 52, alignment: Alignment.center, child: lastData[2]),
                Container(
                    width: 110,
                    alignment: Alignment.center,
                    child: lastData[3]),
                Container(
                  width: 60,
                  alignment: Alignment.center,
                  child: QsText(
                      entity?.lastRate.noValue == true
                          ? ""
                          : "${(double.parse(entity!.lastRate!) * 100).toStringAsFixed(2)}%",
                      fontSize: 12),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget header(int index) {
    return Container(
      height: 32,
      decoration: const BoxDecoration(
          border:
              Border(bottom: BorderSide(width: 0.5, color: Colours.greyEE))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (index == 2) headerTitle(30, '让'),
          Container(width: 30),
          headerTitle(52, logic.headTypeList[index][0]),
          headerTitle(52, logic.headTypeList[index][1]),
          headerTitle(52, logic.headTypeList[index][2]),
          headerTitle(110, '凯利'),
          headerTitle(60, '返还率'),
        ],
      ),
    );
  }

  Widget headerTitle(double width, String text) {
    return Container(
      width: width,
      alignment: Alignment.center,
      child: QsText(text, fontSize: 12),
    );
  }
}
